package com.imdroid.lite.model;

import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.preference.PreferenceManager;

import com.imdroid.lite.IMDroid;

/**
 * The object destination
 */
public class Destination {
	/**
	 * The sp prefix
	 */
	private static final String SP_PREFIX = "Destination_";
	/**
	 * The name of destination
	 */
	public String name;
	/**
	 * The address of destination
	 */
	public String address;
	/**
	 * The longitude of destination
	 */
	public float longitude;
	/**
	 * The latitude of destination
	 */
	public float latitude;

	/**
	 * Get the name of the destination
	 * 
	 * @return the name
	 */
	public String getName() {
		return name;
	}

	/**
	 * Set the name of the destination
	 * 
	 * @param name
	 *            the name to set
	 */
	public void setName(String name) {
		this.name = name;
	}

	/**
	 * Get the address of the destination
	 * 
	 * @return the address
	 */
	public String getAddress() {
		return address;
	}

	/**
	 * Set the address of the destination
	 * 
	 * @param address
	 *            the address to set
	 */
	public void setAddress(String address) {
		this.address = address;
	}

	/**
	 * Get the longitude of the destination
	 * 
	 * @return the longitude
	 */
	public float getLongitude() {
		return longitude;
	}

	/**
	 * Set the longitude of the destination
	 * 
	 * @param longitude
	 *            the longitude to set
	 */
	public void setLongitude(float longitude) {
		this.longitude = longitude;
	}

	/**
	 * Get the latitude of the destination
	 * 
	 * @return the latitude
	 */
	public float getLatitude() {
		return latitude;
	}

	/**
	 * Set the latitude of the destination
	 * 
	 * @param latitude
	 *            the latitude to set
	 */
	public void setLatitude(float latitude) {
		this.latitude = latitude;
	}

	/**
	 * Get the destination
	 * 
	 * @param name
	 *            the name of the destination
	 * @return The destination object
	 */
	public static Destination getDestination(String name) {
		if (name == null || name.length() <= 0)
			return null;

		SharedPreferences prefs = PreferenceManager
				.getDefaultSharedPreferences(IMDroid.getAppContext());
		String text = prefs.getString(SP_PREFIX + name, null);
		if (text == null || text.length() <= 0)
			return null;
		String[] items = text.split(";");
		Destination d = new Destination();
		d.setAddress(items[0]);
		d.setLatitude(Float.parseFloat(items[1]));
		d.setLongitude(Float.parseFloat(items[2]));
		d.setName(items[3]);
		return d;
	}

	/**
	 * Clear the destination
	 * 
	 * @param name
	 *            the name of the destination
	 */
	public static void clearDestination(String name) {
		SharedPreferences prefs = PreferenceManager
				.getDefaultSharedPreferences(IMDroid.getAppContext());
		Editor editor = prefs.edit();
		editor.putString(SP_PREFIX + name, null);
		editor.commit();
		return;
	}

	/**
	 * Save the destination
	 * 
	 * @param destination
	 *            the destination
	 */
	public static void saveDestination(Destination destination) {
		// 将目的地实例化至shared preferences
		SharedPreferences prefs = PreferenceManager
				.getDefaultSharedPreferences(IMDroid.getAppContext());

		Editor editor = prefs.edit();
		StringBuffer sb = new StringBuffer();
		sb.append(destination.getAddress() != null ? destination.getAddress()
				: "");
		sb.append(";");
		sb.append(destination.getLatitude());
		sb.append(";");
		sb.append(destination.getLongitude());
		sb.append(";");
		sb.append(destination.getName());

		editor.putString(SP_PREFIX + destination.getName(), sb.toString());
		editor.commit();
	}

	public static void saveDestinationAsHistory(Destination destination) {
		Destination destCommon;
		if (destination == null) {
			return;
		}

		destCommon = getDestination("common2");
		if (destCommon != null) {
			destCommon.setName("common3");
			saveDestination(destCommon);
		}

		destCommon = getDestination("common1");
		if (destCommon != null) {
			destCommon.setName("common2");
			saveDestination(destCommon);
		}

		destination.setName("common1");
//		String history = IMDroid.getAppContext().getResources()
//				.getString(R.string.loc_history);
		destination.setAddress( destination.getAddress());
		saveDestination(destination);

	}
}
